<?php
	/**
	 * 生成临时Token
	 */
    class CreateToken extends APIBase {
		
		public $tag = "CreateToken";
		public $isLogin = false;
		
		
		public function before() {
			$this->initMysql();
			$this->initDataRedis();
			$this->initCacheRedis();
			$this->InitReportRedis();
			$this->inithttp();
			return true;
		}
	
		public function logic() {
			
			if(!isset($this->param['passport'])){
				$this->returnError(301,'passport is must need!');
			}

			if(!isset($this->param['channel'])){
				$this->returnError(301,'channel is must need!');
			}

			if(!isset($this->param['agent'])){
				$this->returnError(301,'agent is must need!');
			}

			if(!isset($this->param['nickname'])){
				$this->returnError(301,'nickname is must need!');
			}

			if(!isset($this->param['faceUrl'])){
				$this->returnError(301,'faceUrl is must need!');
			}

			if(!isset($this->param['sex'])){
				$this->returnError(301,'sex is must need!');
			}

			if(!isset($this->param['deviceID'])){
				$this->returnError(301,'deviceID is must need!');
			}

			if(!isset($this->param['platform'])){
				$this->returnError(301,'platform is must need!');
			}

			if(!isset($this->param['imei'])){
				$this->returnError(301,'imei is must need!');
			}
			
			if(!isset($this->param['accid'])){
				$this->returnError(301,'accid is must need!');
			}
			
			if(!isset($this->param['account'])){
				$this->returnError(301,'account is must need!');
			}

			//IM用户信息
			$accid = $this->param['accid'];
			$channel = $this->param['channel'];//渠道
			$agent = $this->param['agent'];//代理商
            $nickname = $this->param['nickname'];
			$faceurl = Config::$pic_prefix.$this->param['faceUrl'];
            $sex = $this->param['sex'];
			$deviceID = $this->param['deviceID'];//登录设备号
			$platform = $this->param['platform'];//平台类型
			$imei = $this->param['imei'];//设备型号
			$passport = $this->param['passport'];
			$account = $this->param['account'];//IM账号,判断是否游客
			
			$arr = explode('_',$account);
			if($arr[0]=='gt'){
				$is_visitor = 1;
			}else{
				$is_visitor = 0;
			}
			
            //$this->e($this->param['faceUrl']);
			
			//获取ip
            $ip = $this->getRealIp();

			//判断表中是否存在该用户
			
			$row = $this->mysql->select("player", "*", array('passport' => $passport));
		    $c = count($row);

			//若不存在
			if($c==0){
				/*
			    $where_ip = array('register_ip' => trim($ip));
				$ipdata = $this->mysql->select("player", "COUNT(*) AS `count`", $where_ip);
				if ($ipdata[0]['count'] > 20) {	
					$this->returnError(304, '该设备已经超出可注册用户范围！');
				}
				*/
				$user_info = $this->AutoReg($passport,$nickname,$faceurl,$sex,$ip,$deviceID,$platform,$imei,$channel,$agent,$accid,$is_visitor);
				
				$uid = $user_info['uid'];
				$token = $user_info['token'];
				$register_time = $user_info['register_time'];
				unset($user_info['register_time']);

				//注册数据上报
				$register_data = array('user_id'=>(int)$uid,'cid'=>Config::$info_report['cid'],'platform_type'=>(string)$platform,'imei'=>$imei,'ua'=>$deviceID,
				'register_time'=>$register_time,'channel'=>$channel,'agent'=>$agent,'ip'=>$ip); 
				 $this->report_redis->rpush('register_data',json_encode($register_data));

			    //把token缓存到redis
				$this->data_redis->hMset("user_token:$uid",array('token'=>$token));
				//过期时间300秒
	            $this->data_redis->expire("user_token:$uid",Config::$tokenTime);
                $this->returnData($user_info);

			}else{//玩家已经存在表中
			    //判断用户是否被禁用
			    if ($row[0]['status'] == 0) 
				{
					$this->returnError(310,"账号异常，请联系客服！");
				}

				$uid = $row[0]['id'];
				$this->initDataRedis($uid);
				
				//更新头像
				$this->mysql->update('player',array('avater_url'=>$faceurl),array('passport'=>$passport));
				$isExists = $this->data_redis->exists("hu:{$uid}");
				if($isExists){
					$this->data_redis->hset("hu:{$uid}",'avater_url',$faceurl);
				}
                
				//生成token
				$password = rand(1, 1000000);
                $token = md5($password.$password.time());
				$user_info['uid'] = $uid;
				$user_info['token'] =$token;
				//把token缓存到redis
				$this->data_redis->hMset("user_token:$uid",array('token'=>$token));
				//过期时间300秒
	            $this->data_redis->expire("user_token:$uid",Config::$tokenTime);
                $this->returnData($user_info);
			}
	    }
	   
		private function AutoReg($passport,$nickname,$faceurl,$sex,$ip,$deviceID,$platform,$imei,$channel,$agent,$accid,$is_visitor){
			$password = rand(1, 1000000);			
			$new_user = $this->createUser($passport,$nickname,$faceurl,$sex,$ip,$password,$platform,$deviceID,$imei,$channel,$agent,$accid,$is_visitor);

			$uid = $this->mysql->insert("player", $new_user);
			if($uid==0){
				$this->e("insert user error");
				$this->returnError(500, "server is error");
			}	
			$token = md5($password.$password.time());
			$user_info['uid'] = $uid;
			$user_info['token'] = $token;
			$user_info['register_time'] = $new_user['create_time'];
			return $user_info;
		}


		private function createUser($passport,$nickname,$faceurl,$sex,$ip,$password,$platform,$deviceID,$imei,$channel,$agent,$accid,$is_visitor) {
			$now = time();
			$user_auto = filter_emoji($nickname);
			$this->d($user_auto);
			$password_auto = $password;
			$user_info = array();
			if(strlen($user_auto)<=0){
				$user_auto = 'player';
			}
			$user_info["passport"] = $passport;
			$user_info["accid"] = $accid;
			$user_info["user"] = $user_auto;
			$user_info["name"] = $user_auto;
			$user_info["password"] = md5($password_auto . $password_auto);
			$user_info["salt"] = $password_auto;
			$user_info["status"] = 1; // 0：为封号的
			$user_info["platform_type"]=$platform;//0:安卓 1:IOS
			$user_info["sex"] = $sex;
			$user_info["skey"] = md5(time().$password_auto);//用户登录session
			$user_info["login_days"] = 0;//连续登录的天数
			$user_info["is_get_login_reward"] = 0;//领取连续登录奖励	
			$user_info["heartbeat_at"] = $now;//最后活动时间
			$user_info["create_time"] = $now;//注册时间
			$user_info["update_time"] = $now;//登录时间
			$user_info["avater_url"] = $faceurl;//头像
			$user_info["is_vip"] = 0;
			$user_info["register_ip"] = $ip;
			$user_info["login_ip"] = $ip;
            $user_info["device_num"] = $deviceID;
			$user_info["imei"] = $imei;
			$user_info["channel"] = $channel;//渠道
			$user_info["agent"] = $agent;//代理商
			$user_info['payment'] = 1;//支付方式(1 微信支付 2支付宝支付)
			$user_info['point'] = 0;//玩家分数
			$user_info['is_visitor'] = $is_visitor;//玩家分数
			//$user_info['round_card_num'] = 10;//局数卡
			return $user_info;
	    }
		   
		

    	public function after() {
    		$this->deinitDataRedis();
    		$this->deinitCacheRedis();
    		$this->deinitReportRedis();
			$this->deinitMysql();
			$this->deinithttp();
		}
    }
?>
